#include "GroupChat.h"

GroupChat::GroupChat(int id, string name)
{
	this->id = id;
	this->name = name;
}

void GroupChat::join(User* user)
{
	users.push_back(user);
}

void GroupChat::left(User* user)
{
	for (vector<User*>::iterator it = users.begin(); it != users.end(); ++it) {
		if ( (*it)->getUserId() == user->getUserId() ){
			users.erase(it);
			break;
		}
	}
}

bool GroupChat::isEmpty(){
	return users.empty();
}

void GroupChat::instantMessaging(int userId, string text)
{
	for (vector<User*>::iterator it = users.begin(); it != users.end(); ++it)
	{
		if ( (*it)->getUserId() != userId ){
			message msg;
			msg.mtype = RESPONSE;
			msg.clientId = userId;
			msg.commandId = OK;
			msg.groupChatId = id;
			strcpy( msg.text, text.c_str() );
			(*it)->sendMessage(msg);
		}
	}
}
string GroupChat::usersConnected()
{
	string usersNick;
	for (vector<User*>::iterator it = users.begin(); it != users.end(); ++it)
	{
		usersNick += CommonFunctions::ToString((*it)->getUserId()) + "_" + (*it)->getNick() + " , ";
	}
	return usersNick;
}

int GroupChat::getId(){
	return id;
}

string GroupChat::getName(){
	return name;
}

GroupChat::~GroupChat()
{
//	for (vector<User*>::iterator it = users.begin(); it != users.end(); ++it)
//	{
//		delete *it;
//	}
}
